﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class RoomTypeDAL
    {
        public RoomTypeDAL(HotelContext hotelContext)
        {
            HotelContext = hotelContext;
        }

        public HotelContext HotelContext { get; }
        /// <summary>
        /// 添加房型
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int RoomTypeAdd(RoomTypeModel info)
        {
            try
            {
                var res = HotelContext.roomTypeModels.Count(o => o.RoomTypName == info.RoomTypName);
                if (res > 0)
                {
                    return -1;//房型名称不能重复
                }
                HotelContext.roomTypeModels.Add(info);
                return HotelContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示查询分页
        /// </summary>
        /// <param name="totalCount"></param>
        /// <param name="totalPage"></param>
        /// <param name="Pageindex"></param>
        /// <param name="Pagesize"></param>
        /// <param name="RoomTypName"></param>
        /// <returns></returns>
        public List<RoomTypeModel> GetRoomTypeList(out int totalCount,out int totalPage,int Pageindex=1,int Pagesize=2,string? RoomTypName="")
        {
            try
            {
                var list = HotelContext.roomTypeModels.Where(o => o.IsDelete == false).ToList();
                if (!string.IsNullOrEmpty(RoomTypName))
                {
                    list =list.Where(o=>o.RoomTypName.Contains(RoomTypName)).ToList();
                }
                totalCount = list.Count();
                totalPage = (int)Math.Ceiling(totalCount * 1.0 / Pagesize);
                list=list.OrderByDescending(o=>o.Id).Skip((Pageindex-1)*Pagesize).Take(Pagesize).ToList();
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 房型Id查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public RoomTypeModel GetRoomTypeId(int id)
        {
            try
            {
                return HotelContext.roomTypeModels.Find(id);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int RoomTypeDel(int id)
        {
            try
            {
                var res = HotelContext.roomTypeModels.Find(id);
                if (res != null)
                {
                    res.IsDelete = true;
                }
                return HotelContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 房型修改
        /// </summary>
        /// <param name="roomType"></param>
        /// <returns></returns>
        public int RoomTypeUpdate(RoomTypeModel info)
        {
            try
            {
                var list = HotelContext.roomTypeModels.Count(o => o.IsDelete == false && o.RoomTypName == info.RoomTypName && o.Id != info.Id);
                if (list > 0)
                {
                    return -1;
                }
                HotelContext.roomTypeModels.Update(info);
                return HotelContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
